package com.janko.springbootbase.a_mainTest;

import cn.hutool.core.date.DateTime;
import org.springframework.util.CommonsLogWriter;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * @Author nyk
 * @Date 2021/6/3 15:35
 * @Version 1.0
 * @Desc
 */
public class MainFun {
    public static void main(String[] args) throws InterruptedException, ParseException {
//        Map<String,Object> map=new HashMap<>();
//        map.put("a","");
//        map.put("b","12");
//        map.put("c","12.04");
//        map.put("d","12.04a");
//        System.out.println(getObjectToString(map.get("a")));
//        System.out.println(getObjectToString(map.get("b")));
//        System.out.println(getObjectToString(map.get("c")));
//        System.out.println(getObjectToString(map.get("d")));
//        completableFutureTest();
//        forDemo();

        SimpleDateFormat sdfAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdfAll.parse("2019-01-31 00:00:00");
//        Date date2 = sdfDay.parse("2019-01-31 00:00:30");
        Date date2 = new Date();
        if (date1.before(date2)) {
            System.out.println("before ke yong");
        } else {
            System.out.println("before 不可用");
        }

        System.out.println("date1.getTime()的值是：{" + date1.getTime() + "}");
        System.out.println("date2.getTime()的值是：{" + date2.getTime() + "}");
        if (date1.getTime() < date2.getTime()) {
            System.out.println("before ke yong");
        } else {
            System.out.println("before 不可用 ");
        }
    }

    private static void forDemo() {
        long start = System.currentTimeMillis();
        List<String> param = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        for (String s : param) {
            doSomeThing();
            System.out.println(s);
        }
        System.out.println("循环遍历结束");
        System.out.println(String.format("本次执行耗时:%d", System.currentTimeMillis() - start));
    }

    private static void completableFutureTest() {
        long start = System.currentTimeMillis();
        List<String> param = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
        List<CompletableFuture> list = new ArrayList<>();

        for (String s : param) {
            list.add(CompletableFuture.runAsync(() -> {
                doSomeThing();
                System.out.println(Thread.currentThread().getName() + ": " + s);
            }));
        }

        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()]));

        completableFuture.thenRun(() -> System.out.println("循环遍历结束"));
        completableFuture.join();

        System.out.println(String.format("本次执行耗时:%d", System.currentTimeMillis() - start));
    }

    private static void doSomeThing() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void doUploadFile(String param) {
        System.out.println(LocalDateTime.now() + "执行参数" + param);
    }

    private static String getObjectToString(Object obj) {
        if (StringUtils.isEmpty(obj)) return "0";
        try {
            Double ddd = Double.parseDouble(obj.toString());
            return String.valueOf(ddd.doubleValue());
        } catch (Exception ex) {
            return "0";
        }
    }

    static class PPP {
        private BigDecimal amount;

        public BigDecimal getAmount() {
            return amount;
        }

        public void setAmount(BigDecimal amount) {
            this.amount = amount;
        }
    }
}
